using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace stsdev {
  public partial class SelectSolutionType : Form {

    public SelectSolutionType() {
      InitializeComponent();
    }

    private void SelectSolutionType_Load(object sender, EventArgs e) {
      // load default folder path 
      RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("TargetFolder");
      object defaultFolder = defaultFolderKey.GetValue("");
      if (defaultFolder != null && Directory.Exists(defaultFolder.ToString())) {
        txtParentDirectory.Text = defaultFolder.ToString();
      }
      else {
        txtParentDirectory.Text = @"c:\";
      }
      // load default solution version type
      RegistryKey defaultSolutionVersionType = Application.UserAppDataRegistry.CreateSubKey("SolutionVersionType");
      object solutionVersionType = defaultSolutionVersionType.GetValue("");
      if (solutionVersionType != null) {
        switch (solutionVersionType.ToString()) {
          case "VS2005":
            radVS2005.Checked = true;
            break;
          case "VS2008_30":
            radVS2008_30.Checked = true;
            break;
          case "VS2008_35":
            radVS2008_35.Checked = true;
            break;
        }
        // update UI
        EnableCreateButton();
        // set txtSolutionName as control with focus
        txtSolutionName.Focus();
      }
      // load default signing key
      RegistryKey defaultSigningKeyFileKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
      object solutionSigningKeyFile = defaultSigningKeyFileKey.GetValue("");
      if (solutionSigningKeyFile != null && File.Exists(solutionSigningKeyFile.ToString())) {
        txtSigningKey.Text = solutionSigningKeyFile.ToString();
      }      
    }

    public string GetSolutionName() {
      return txtSolutionName.Text;
    }

    public string GetParentDirectory() {
      return txtParentDirectory.Text;
    }

    public string GetSigningKeyFile() {
      return txtSigningKey.Text;
    }

    public ISolutionProvider GetSelectedProvider() {
      return (ISolutionProvider)lstSolutionTypes.SelectedItem;
    }

    public SolutionVersionTypeEnum GetSolutionVersionType() {
      if (radVS2008_30.Checked)
        return SolutionVersionTypeEnum.VS2008_30;
      if (radVS2008_35.Checked)
        return SolutionVersionTypeEnum.VS2008_35;
      else
        return SolutionVersionTypeEnum.VS2005;
    }

    public void InitializeSolutionProviders(IList<ISolutionProvider> SolutionProviders) {
      lstSolutionTypes.DisplayMember = "Title";
      foreach (ISolutionProvider SolutionProvider in SolutionProviders) {
        lstSolutionTypes.Items.Add(SolutionProvider);
      }
      lstSolutionTypes.SelectedIndex = 0;
    }
 
    private void cmdChangeParentDirectory_Click(object sender, EventArgs e) {
      dlgFolderBrowser.SelectedPath = txtParentDirectory.Text;
      DialogResult result = dlgFolderBrowser.ShowDialog();
      if (result == DialogResult.OK) {
        txtParentDirectory.Text = dlgFolderBrowser.SelectedPath;
        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("TargetFolder");
        defaultFolderKey.SetValue("", dlgFolderBrowser.SelectedPath);
      
      }
    }

    private void cmdChangeSigningKey_Click(object sender, EventArgs e) {
      SelectSigningKey dlg = new SelectSigningKey();
      DialogResult result = dlg.ShowDialog();
      if (result == DialogResult.OK) {
        txtSigningKey.Text = dlg.SigningKeyFile;
      }
    }

    private void SolutionVersionType_Changed(object sender, EventArgs e) {
      RegistryKey defaultSolutionVersionType = Application.UserAppDataRegistry.CreateSubKey("SolutionVersionType");
      if (radVS2005.Checked)
        defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2005.ToString());
      if (radVS2008_30.Checked)
        defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2008_30.ToString());
      if (radVS2008_35.Checked)
        defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2008_35.ToString());
    }

    private void txt_TextChanged(object sender, EventArgs e) {
      EnableCreateButton();
    }

    private void EnableCreateButton(){
      if ( (txtSolutionName.Text.Length > 0) &&
           (txtParentDirectory.Text.Length > 0) &&
           (txtSigningKey.Text.Length > 0) ){
        cmdCreateSolution.Enabled = true;
      }
      else {
        cmdCreateSolution.Enabled = false;
      }
    }



  }
}